package control
{
	import data.DataBase;
	
	import events.DamageToHero;
	import events.MonsterDieEvent;
	
	import flash.events.Event;
	import flash.geom.Point;
	
	import stages.Stage_lv1;
	
	import utils.XMLHelper;
	
	import view.Monster;
	
	public class MonsterControl extends MonsterMove
	{
		public function MonsterControl(_monsterXml:XMLList,_monsterArr:Array,_lineArr:Array)
		{
			super(_lineArr,_monsterArr)
			
			xmlList=_monsterXml
			//监听死亡事件并且执行移除
			addEventListener("died",removeView);
			
			speed = _monsterXml.@moveSpeed;
			speed2 = _monsterXml.@moveSpeed2;
			monsterMotion = new Monster(_monsterXml);
			addChild(monsterMotion);
			monsterMotion.hpLineLong = hp
			monsterMotion.direction = direction;
			addEventListener(Event.ENTER_FRAME,updatePosition)
			addEventListener("atted",att);
		}
		
		protected function att(event:Event):void
		{
//			var evt:DamageToHero = new DamageToHero(DamageToHero.DAMAGE_TO_HERO,hero,attNum,true);
		//	dispatchEvent(evt);
		}
		private var attNum:uint = 4;
		/**
		 *价格 
		 */		
		private var price:uint = 20;
		/**
		 *受到的伤害 
		 */		
		private var _damage:Number;
		/**
		 *怪物的数据xml 
		 */		
		private var xmlList:XMLList;
		/**
		 *怪物的显示对象 
		 */		
		private var monsterMotion:Monster;
		/**
		 *怪物的血条 
		 */		
		private var hp:Number=50;
		
		/**
		 *删除显示对象Monster类
		 * 抛出删除自己的事件 
		 * @param event
		 * 
		 */		
		public function removeView(event:Event):void
		{
			removeChild(monsterMotion);
			monsterMotion = null;
			xmlList = null;
			var evt:MonsterDieEvent = new MonsterDieEvent(MonsterDieEvent.MONSTER_DIE_EVENT,this,true);
			dispatchEvent(evt);
		}
		/**
		 *更新怪物位置 
		 * @param event
		 * 
		 */		
		protected function updatePosition(event:Event):void
		{
			
			x = crtPoint.x;
			y = crtPoint.y;
			monsterMotion.direction = direction;
			if(direction ==4)
			{
				removeEventListener(Event.ENTER_FRAME,updatePosition)
			}
			
		}		
		public function get damage():Number
		{
			return _damage;
		}
		
		public function set damage(value:Number):void
		{
			_damage = value;
			
			hp -= _damage;						// 扣血量
			
			if(hp<=0)
			{
				hp = 0;
				Stage_lv1.updateMoney(price);
				removeEventListener(Event.ENTER_FRAME,updatePosition);
				remove();						//删除数据类
				monsterMotion.direction = 4;	//播放死亡动画
			}
			monsterMotion.hpLineLong = hp		//更新血条显示
			
		}
/*
		public function get hero():HeroControl
		{
			return _hero;
		}

		public function set hero(value:HeroControl):void
		{
			_hero = value;
			if(hp>0)
			{
				removeEventListener(Event.ENTER_FRAME,updatePosition);
				monsterMotion.direction = 6;
				stopMoving();
			}
		}
		*/
		private function stopMoving():void
		{
			speed2 = 0;
		}
		
		//private var _hero:HeroControl
		
	}
}